/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package restaurante;

import java.awt.Transparency;
import java.util.Collection;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.InnerShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.*;

/**
 *
 * @author 66785403
 */
public class PanelFiguras extends VBox {
    
    private Button circle;
    private Button cuadrado;
    private Salon draw;
    private Button rectangle;
    private BorderPane layerPane;
    private Restaurante rest;
    private Button ele;
    final DropShadow dropShadow = new DropShadow();
    private ShapeFactory shapeFactory;
    
    public PanelFiguras(Salon draw, BorderPane layerPane_, Restaurante rest_) {
        super(10);
        setId("shape-panel");
        this.draw = draw;
        this.layerPane = layerPane_;
        this.rest = rest_;
        dropShadow.setRadius(10.0);
        dropShadow.setOffsetX(3.0);
        dropShadow.setOffsetY(3.0);
        addChildren();
        setPrefWidth(100);
        setMinWidth(100);
        shapeFactory = ShapeFactory.getInstance();
    }
    
    private void addChildren() {
        ObservableList<Node> children = getChildren();
        
        circle = new Button("", createShape(new Circle(20)));
        circle.setOnMouseClicked(new ShapeHandler("circular"));
        
        cuadrado = new Button("", createShape(new Rectangle(36, 36)));
        cuadrado.setOnMouseClicked(new ShapeHandler("cuadrada"));
        
        rectangle = new Button("", createShape(new Rectangle(22, 38)));
        rectangle.setOnMouseClicked(new ShapeHandler("rectangular"));
        
        ele = new Button("", createShape(new Rectangle(0, 0, 15, 25), new Rectangle(0, 20, 25, 15)));
        ele.setOnMouseClicked(new ShapeHandler("ele"));
        
        configureButtons(circle, cuadrado, rectangle, ele);
        children.addAll(circle, cuadrado, rectangle, ele);

        //Pane eventHandler

    }
    
    private Node createShape(Shape... shapes) {
        Shape totalShape = null;
        Shape lastShape = null;
        for (Shape shape : shapes) {
            if (lastShape != null) {
                if (totalShape == null) {
                    totalShape = Path.union(lastShape, shape);
                } else {
                    totalShape = Path.union(totalShape, shape);
                }
            }
            lastShape = shape;
            if (shapes.length == 1) {
                totalShape = shape;
            }
        }
        totalShape.setEffect(dropShadow);
        totalShape.setSmooth(true);
        totalShape.setStroke(Color.WHITE);
        totalShape.setStrokeWidth(2);
        totalShape.setFill(null);
        totalShape.setStrokeType(StrokeType.INSIDE);
        totalShape.setStrokeLineJoin(StrokeLineJoin.BEVEL);
        totalShape.setStrokeMiterLimit(1);
        return totalShape;
    }
    
    private void configureButtons(Button... buttons) {
        for (Button button : buttons) {
            button.setPrefSize(50, 50);
            button.setMinSize(50, 50);
            button.setMaxSize(50, 50);
        }
    }
    
    private class ShapeHandler implements EventHandler<MouseEvent> {
        
        private String shapeTipe;
        
        public ShapeHandler(String shapeTipe_) {
            shapeTipe = shapeTipe_;
        }
        
        @Override
        public void handle(MouseEvent t) {
            Area shape = shapeFactory.getMesa(shapeTipe);
            rest.sp.setPannable(false);
            shape.setSalon(draw);
            rest.setMesa(shape, t.getSceneX(), t.getSceneY());
        }
    }
}
